package layui.layui.service;

import layui.layui.dao.BookDao;
import layui.layui.entity.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BookService {
    @Autowired
    BookDao bookDao;

    public List<Books> getBooks(String keywd){
        return bookDao.getBooks(keywd);
    }

    public void deleteBooks(String ids) {
        if(ids == null || ids == "") {
            return;
        }
        int[] ids_ = Arrays.stream(ids.split(";")).mapToInt(a -> {return new Integer(a);}).toArray();
        if(ids_ == null || ids_.length < 1) {
            return;
        }
//        System.out.println(Arrays.stream(ids_).iterator());
        bookDao.deleteBooks(ids_);
    }

    public void editBookName(String name, int id) {
        bookDao.editBookName(name, id);
    }

    public void editCount(int count, int id) {bookDao.editCount(count, id);}

    public String getName(int id) {return bookDao.getName(id);}

    @Transactional
    public void addBorrow(int stuId, int bookId){
        bookDao.addOrder(stuId, bookId);
//        int i = 1/0;
        bookDao.minusCount(bookId, 1);

    };
}
